<?php
/**
 * User: lsx
 * Date: 2017/5/17
 * Time: 9:27
 * Progress:0%
 */

namespace logic\wechat\index;


use library\common\str;
use model\Account;
use model\ShopRegUserOrder;
use model\ShopStore;
use model\ShopUserAddress;

use think\Config;
use think\Cookie;
use think\Db;

class LUser
{
    private $aid;
    private $account;

    private $shopUserAddress;
    private $shopWatercardOrder;
    public function __construct()
    {
        $this->aid = Cookie::get(Config::get('const.cookie_name_wechat'));
        $this->account=new Account();

        $this->shopUserAddress=new ShopUserAddress();


    }

    public function logic_set_pay_password(){

    }

    public function logic_set_pay_password_save($param){
        if($param['password'] != $param['password_confirm']){
            return array(
                'success'=>false,
                'message'=>'两次密码不一致'
            );
        }
        $data=array(
            'pay_password'=>md5($param['password'])
        );
        $res=$this->account->save($data,['id'=>$this->aid]);
        return array(
            'success'=>true,
            'message'=>'保存成功'
        );
    }

    public function logic_vipcard_order(){

    }

    public function logic_card_order_detail($id){
        $shopRegUserOrder=new ShopRegUserOrder();
        $data=$shopRegUserOrder->where(['id'=>$id])->find();
        return $data;
    }

    public function logic_vipcard_order_detail($id){

    }

    public function logic_card_order(){
        $shopRegUserOrder=new ShopRegUserOrder();
        $data=$shopRegUserOrder->where(['account_id'=>$this->aid])->select();
        return $data;
    }

    public function logic_wc(){

    }

    public function logic_wc_order_list(){
        $order=$this->shopWatercardOrder->alias('s')
            ->join('shop_user_watercard w','w.id=s.user_card_id','left')
            ->join('shop_watercard c','c.id=w.card_id','left')
            ->field("s.*,w.card_sn,c.name,(case 
        when (order_status = 000 ) then '下单成功，等待配送' 
        when (order_status = 110 or order_status = 210 ) then '配送中'
        when (order_status = 111 or order_status = 211 ) then '订单完成'
        else order_status end)
        as 'order_status' ")->where(['s.account_id'=>$this->aid])->order('s.order_time desc')->select();
        return $order;
    }

    public function logic_wc_order_submit($user_card_id){

    }
    //水卡订水下单提交
    public function logic_wc_order_submit_save($param){
        $adr=$this->shopUserAddress->where(['id'=>$param['address_id']])->find();
        //水卡订单表插入一条数据
        $order_data=array(
            'store_id'=>$param['store_id'],
            'account_id'=>$this->aid,
            'user_card_id'=>$param['user_card_id'],
            'amount'=>$param['order_count'],
            'order_sn'=>str::orderNo(),
            'order_time'=>time(),
            'o_province'=>$adr->province,
            'o_city'=>$adr->city,
            'o_district'=>$adr->district,
            'o_address'=>$adr->address,
            'o_name'=>$adr->name,
            'o_mobile'=>$adr->mobile,
            'o_lng'=>$adr->lng,
            'o_lat'=>$adr->lat,
            'o_zoom'=>$adr->zoom,
            'o_zipcode'=>$adr->zip_code,
            'order_status'=>'000',
        );
        Db::startTrans();
        $res1=$this->shopWatercardOrder->insertGetId($order_data);
        if(!$res1){
            Db::rollback();
            return array(
                'success'=>false,
                'message'=>'数据出错1，请稍后再试'
            );
        }

        //todo 写入日志
        Db::commit();
        //发消息给配送员
        return array(
            'success'=>true,
            'message'=>'订水成功',
            'data'=>array(
                'order_id'=>$res1
            )
        );
    }

    public function wc_order_detail($id){
        return $this->shopWatercardOrder->alias('s')
                ->join('shop_user_watercard w','w.id=s.user_card_id','left')
                ->join('shop_watercard c','w.card_id=c.id','left')
                ->join('account a','s.delivery_id=a.id','left')
                ->join('shop_goods g','c.goods_id=g.id','left')
                ->join('shop_store t','s.store_id=t.id','left')
                ->field("s.*,a.name as delivery_name,a.mobile as delivery_mobile,g.name as goods_name,t.name as store_name,(case 
                when (order_status = 000 ) then '下单成功，等待配送' 
                when (order_status = 110 or order_status = 210 ) then '配送中'
                when (order_status = 111 or order_status = 211 ) then '订单完成'
                else order_status end)
                as 'order_status'")
                ->where(['s.id'=>$id])
                ->find();
    }

    public function logic_user_address(){
        $shopUserAddress=new ShopUserAddress();
        $item=$shopUserAddress->where(['account_id'=>$this->aid,'flag_default'=>1])->find();
        return $item;
    }

    public function logic_shop_store(){
        $shopUserAddress=new ShopUserAddress();
        $item=$shopUserAddress->where(['account_id'=>$this->aid,'flag_default'=>1])->find();
        //收货地址坐标
        $to_lat=$item->lat;
        $to_lng=$item->lng;
        //店铺坐标
        $shopStore=new ShopStore();

        $store=$shopStore->field("id,name,lat,lng, ROUND(
        6378.138 * 2 * ASIN(
            SQRT(
                POW(
                    SIN(
                        (
                            $to_lat * PI() / 180 - lat * PI() / 180
                        ) / 2
                    ),
                    2
                ) + COS($to_lat * PI() / 180) * COS(lat * PI() / 180) * POW(
                    SIN(
                        (
                            $to_lng * PI() / 180 - lng * PI() / 180
                        ) / 2
                    ),
                    2
                )
            )
        ) 
   ,2 ) AS distance_um")->order('distance_um asc')->select();
        return $store;

    }
    private function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters

        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;

        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;

        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;

        return round($calculatedDistance);
    }


}